<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible"
    @closed="dialogClose"
  >
    <el-form
      :model="dataForm"
      size="small"
      :rules="dataRule"
      ref="dataForm"
      label-width="120px"
      @keyup.enter.native="dataFormSubmit()"
    >
      <el-form-item label="属性名" prop="name">
        <el-input v-model="dataForm.name" placeholder="属性名"></el-input>
      </el-form-item>
      <el-form-item label="属性规格" prop="type">
        <el-radio-group v-model="dataForm.type" :disabled="typeShow">
          <el-radio-button label="1">规格属性</el-radio-button>
          <el-radio-button label="0">销售属性</el-radio-button>
        </el-radio-group>
      </el-form-item>

      <el-form-item label="属性类型" prop="multipleState">
        <el-radio-group v-model="dataForm.multipleState">
          <el-radio-button label="0">单值</el-radio-button>
          <el-radio-button label="1">多值</el-radio-button>
        </el-radio-group>
      </el-form-item>

      <el-form-item label="属性值" prop="valueList">
        <el-select
          style="width: 100%"
          v-model="dataForm.valueList"
          multiple
          filterable
          allow-create
          placeholder="属性值"
        >
          <el-option
            v-for="item in dataForm.valueList"
            :label="item"
            :key="item"
            :value="item"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="属性图标" prop="icon">
        <el-input v-model="dataForm.icon" placeholder="属性图标"></el-input>
      </el-form-item>
      <el-form-item label="显示状态" prop="showState">
        <el-radio-group v-model="dataForm.showState">
          <el-radio :label="0">不显示</el-radio>
          <el-radio :label="1">显示</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-row>
        <el-col :span="13">
          <el-form-item label="是否检索" prop="searchState">
            <el-radio-group v-model="dataForm.searchState">
              <el-radio :label="0">否</el-radio>
              <el-radio :label="1">是</el-radio>
            </el-radio-group>
          </el-form-item>
        </el-col>
        <el-col :span="11">
          <el-form-item label="属性状态" prop="dataState">
            <el-radio-group v-model="dataForm.dataState">
              <el-radio :label="0">显示</el-radio>
              <el-radio :label="-1">禁用</el-radio>
            </el-radio-group>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
import { addOrUpdateProductAttr } from "@/api/product";
export default {
  data() {
    return {
      visible: false,
      typeShow: false,
      dataForm: {
        id: 0,
        name: "",
        searchState: 0,
        multipleState: "",
        icon: "",
        valueList: [],
        type: "",
        showState: 1,
        dataState: 0,
      },
      dataRule: {
        name: [{ required: true, message: "属性名不能为空", trigger: "blur" }],
        searchState: [
          {
            required: true,
            message: "是否需要检索不能为空",
            trigger: "blur",
          },
        ],
        multipleState: [
          {
            required: true,
            message: "值类型不能为空",
            trigger: "change",
          },
        ],
        type: [
          {
            required: true,
            message: "属性类型不能为空",
            trigger: "change",
          },
        ],
      },
    };
  },
  components: {},
  methods: {
    init(data) {
      this.dataForm.id = 0;
      this.typeShow = false;

      this.visible = true;
      this.$nextTick(() => {
        this.$refs["dataForm"].resetFields();

        if (data && data.id) {
          this.typeShow = true;
          this.dataForm.id = data.id;
          this.dataForm.name = data.name;
          this.dataForm.searchState = data.searchState ? 1 : 0;
          this.dataForm.multipleState = data.multipleState ? 1 : 0;
          this.dataForm.showState = data.showState ? 1 : 0;
          this.dataForm.icon = data.icon;
          this.dataForm.valueList = data.valueList.split(";");
          this.dataForm.type = data.type;
          this.dataForm.dataState = data.dataState;
        }
      });
    },
    // 表单提交
    dataFormSubmit() {
      this.$refs["dataForm"].validate((valid) => {
        if (valid) {
          addOrUpdateProductAttr({
            id: this.dataForm.id || "",
            name: this.dataForm.name,
            searchState: this.dataForm.searchState == 1,
            multipleState: this.dataForm.multipleState == 1,
            showState: this.dataForm.showState == 1,
            icon: this.dataForm.icon,
            valueList: this.dataForm.valueList.join(";"),
            type: this.dataForm.type,
            dataState: this.dataForm.dataState,
          }).then((res) => {
            if (res && res.code === 0) {
              this.$message({
                message: "操作成功",
                type: "success",
                duration: 1500,
                onClose: () => {
                  this.visible = false;
                  this.$emit("refreshDataList");
                },
              });
            } else {
              this.$message.error(res.msg);
            }
          });
        }
      });
    },
    //dialogClose
    dialogClose() {},
  },
};
</script>
